﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DevPhone.Controls;
using DevPhoneToolkitSample.Data;
using Microsoft.Phone.Controls;

namespace DevPhoneToolkitSample.Samples
{
    public partial class CalendarControlColorsSample : PhoneApplicationPage
    {
        private static IEnumerable<ColorItem> CreateListColors()
        {
             return new List<ColorItem>()
             {
                 new ColorItem {Text="Default", Color = Colors.Transparent},
                 new ColorItem {Text="White", Color = Colors.White},
                 new ColorItem {Text="Black", Color = Colors.Black},
                 new ColorItem {Text="DarkGray", Color = Colors.DarkGray},
                 new ColorItem {Text="Gray", Color = Colors.Gray},
                 new ColorItem {Text="LightGray", Color = Colors.LightGray},
                 new ColorItem {Text="Orange", Color = Colors.Orange},
                 new ColorItem {Text="Red", Color = Colors.Red},
                 new ColorItem {Text="Blue", Color = Colors.Blue},
                 new ColorItem {Text="Magenta", Color = Colors.Magenta},
                 new ColorItem {Text="Purple", Color = Colors.Purple},
                 new ColorItem {Text="Green", Color = Colors.Green},
                 new ColorItem {Text="Cyan", Color = Colors.Cyan},                      
                 new ColorItem {Text="Brown", Color = Colors.Brown},
                 new ColorItem {Text="Yellow", Color = Colors.Yellow},
             };
        }

        public CalendarControlColorsSample()
        {
            InitializeComponent();
            CboColor.ItemsSource = CreateListColors();
        }

        private void CalendarControlChangeSelectDate(object sender, DevPhone.Controls.CalendarChangeSelectDateEventArgs e)
        {
            MessageBox.Show("Selected Date: " + e.Date.ToShortDateString());
        }


        private void CboPropertySelectionChange(object sender, SelectionChangedEventArgs e)
        {
            var auxlist = CreateListColors().ToArray();
            var prop = e.AddedItems[0].ToString();
            Color propbrush = Colors.Transparent;
            if (prop == "CalendarBorderBrush")
            {
                propbrush = ((SolidColorBrush)Calend.CalendarBorderBrush).Color;
            }
            if (prop == "CalendarBackground")
            {
                propbrush = ((SolidColorBrush)Calend.CalendarBackground).Color;
            }
            if (prop == "CalendarForeground")
            {
                propbrush = ((SolidColorBrush)Calend.CalendarForeground).Color;
            }
            if (prop == "CurrentDateBrush")
            {
                propbrush = ((SolidColorBrush)Calend.CurrentDateBrush).Color;
            }
            if (prop == "WeekBrush")
            {
                propbrush = ((SolidColorBrush)Calend.WeekBrush).Color;
            }
            if (prop == "WeekBackground")
            {
                propbrush = ((SolidColorBrush)Calend.WeekBackground).Color;
            }
            var found = false;
            foreach (var item in CboColor.ItemsSource)
            {
                if (((ColorItem)item).Color.Equals(propbrush))
                {
                    CboColor.SelectedItem = item;
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                CboColor.SelectedIndex = 0;
            }
        }

        private void CboColorSelectionChange(object sender, SelectionChangedEventArgs e)
        {
            if (CboProperty.SelectedItem == null) return;

            var selcolor = new SolidColorBrush(((ColorItem)e.AddedItems[0]).Color);
            if (CboColor.SelectedIndex == 0)
            {
                selcolor = null;
            }

            var prop =  CboProperty.SelectedItem.ToString();
            if (prop == "CalendarBorderBrush")
            {
                Calend.CalendarBorderBrush = selcolor;
            }
            if (prop == "CalendarBackground")
            {
                Calend.CalendarBackground = selcolor;
            }
            if (prop == "CalendarForeground")
            {
                Calend.CalendarForeground = selcolor;
            }
            if (prop == "CurrentDateBrush")
            {
                Calend.CurrentDateBrush = selcolor;
            }
            if (prop == "WeekBrush")
            {
                Calend.WeekBrush = selcolor;
            }
            if (prop == "WeekBackground")
            {
                Calend.WeekBackground = selcolor;
            }
        }

    }
}